package com.frontend.trade.Service.Impl;


import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.frontend.trade.Service.QRCodeService;
import com.frontend.trade.config.QRCodeConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.order.frontend.feginapi.trade.enums.PayChannelEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
@Component
public class QRCodeServiceImpl  implements QRCodeService {


    @Override
    public String generate(String content, PayChannelEnum payChannel) {
        QrConfig qrConfig = new QrConfig();

      QRCodeConfig qrCodeConfig = new QRCodeConfig();
        //设置边距
        qrConfig.setMargin(qrCodeConfig.getMargin());
        //二维码颜色
        qrConfig.setForeColor(HexUtil.decodeColor(qrCodeConfig.getForeColor()));
        //设置背景色
        qrConfig.setBackColor(HexUtil.decodeColor(qrCodeConfig.getBackColor()));
        //纠错级别
        qrConfig.setErrorCorrection(ErrorCorrectionLevel.valueOf(qrCodeConfig.getErrorCorrectionLevel()));
        //设置宽
        qrConfig.setWidth(qrCodeConfig.getWidth());
        //设置高
        qrConfig.setHeight(qrCodeConfig.getHeight());
        if (ObjectUtil.isNotEmpty(payChannel)) {
            //设置logo
            qrConfig.setImg(qrCodeConfig.getLogo(payChannel));
        }
        return QrCodeUtil.generateAsBase64(content, qrConfig, ImgUtil.IMAGE_TYPE_PNG);
    }

    @Override
    public String generate(String content) {
        return "";
    }
}
